package com.huidutek.flutter_trackingio

import android.app.Activity
import android.app.Application
import android.content.Context
import androidx.annotation.NonNull
import com.reyun.tracking.sdk.Tracking
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result


/** FlutterTrackingioPlugin */
class FlutterTrackingioPlugin : FlutterPlugin, MethodCallHandler {
    private lateinit var channel: MethodChannel
    private lateinit var context: Context

    override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
        channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_trackingio")
        channel.setMethodCallHandler(this)
        context = flutterPluginBinding.applicationContext
    }

    override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
        when (call.method) {
            "setDebugMode" -> {
                Tracking.setDebugMode(call.argument<Boolean>("debug") ?: true);
                result.success(null)
            }
            "initWithKeyAndChannelId" -> {
                Tracking.initWithKeyAndChannelId(
                    this.context.applicationContext as Application,
                    call.argument<String>("appKey"),
                    call.argument<String>("channelId")
                )
                result.success(null)
            }
            "setRegisterWithAccountID" -> {
                Tracking.setRegisterWithAccountID(
                    call.argument<String>("accountId")
                )
            }
            "setLoginSuccessBusiness" -> {
                Tracking.setLoginSuccessBusiness(
                    call.argument<String>("accountId")
                )
            }
            "setPayment" -> {
                call.argument<Float>("currencyAmount")?.let {
                    Tracking.setPayment(
                        call.argument<String>("transactionId"),
                        call.argument<String>("paymentType"),
                        call.argument<String>("currencyType"),
                        it
                    )
                }
            }
            "setEvent" -> {
                Tracking.setEvent(
                    call.argument<String>("eventName")
                )
            }
            "setOrder" -> {
                call.argument<Float>("currencyAmount")?.let {
                    Tracking.setOrder(
                        call.argument<String>("transactionId"),
                        call.argument<String>("currencyType"),
                        it
                    )
                }
            }
            "setAdShow" -> {
                Tracking.setAdShow(
                    call.argument<String>("adPlatform"),
                    call.argument<String>("adId"),
                    call.argument<String>("fill")
                )
            }
            "setAdClick" -> {
                Tracking.setAdClick(
                    call.argument<String>("adPlatform"),
                    call.argument<String>("adId")
                )
            }
            "setAppDuration" -> {
                call.argument<Long>("duration")?.let {
                    Tracking.setAppDuration(it)
                }
            }
            "setPageDuration" -> {
                call.argument<Long>("duration")?.let {
                    Tracking.setPageDuration(
                        call.argument<String>("activityName"),
                        it
                    )
                }
            }
            "exitSdk" -> {
                Tracking.exitSdk()
            }
            else -> {
                result.notImplemented()
            }
        }
    }

    override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
        channel.setMethodCallHandler(null)
    }
}
